package com.gopay.solution.manager.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.cps.dao.solution.CpsAcctProdMastQueryDAO;
import com.gopay.common.cps.dao.solution.CpsProductLineInfoQueryDAO;
import com.gopay.common.domain.acps.AcpsAcctMast;
import com.gopay.common.domain.cps.CpsAcctProdMast;
import com.gopay.solution.dao.smelly.AcpsAcctMastTempQueryDAO;
import com.gopay.solution.domain.vo.AcctTreeNode;
import com.gopay.solution.manager.AcctTreeModelManager;

/**
 * 产品树模型管理器
 * 
 * @AcctTreeModelManagerImpl.java
 * @author MA HUAN
 * @2013-3-26 下午4:02:18 www.gopay.com.cn Inc.All rights reserved.
 */

@Service(value = "acctTreeModelManager")
public class AcctTreeModelManagerImpl implements AcctTreeModelManager {

    @Resource(name = "acpsAcctMastTempQueryDAO")
    private AcpsAcctMastTempQueryDAO acpsAcctMastTempQueryDAO;

    @Resource(name = "cpsAcctProdMastQueryDAO")
    private CpsAcctProdMastQueryDAO cpsAcctProdMastQueryDAO;

    @Resource(name = "cpsProductLineInfoQueryDAO")
    private CpsProductLineInfoQueryDAO cpsProductLineInfoQueryDAO;

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.gopay.solution.manager.AcctTreeModelManager#retrieveFromAcct(java
     * .lang.String)
     */
    @Override
    public AcctTreeNode retrieveFromAcct(String acctId) {

        // 创建账户节点
        AcctTreeNode acctNode = new AcctTreeNode(AcctTreeNode.TYPE.ACCT);
        acctNode.setData(acctId);

        if (StringUtils.isNotEmpty(acctId)) {
            AcpsAcctMast acct = acpsAcctMastTempQueryDAO.find(AcpsAcctMast.class, acctId);
            if (acct != null) {
                String prodCode = acct.getProdCode().toString();

                // 创建账户产品节点
                AcctTreeNode prodNode = new AcctTreeNode(AcctTreeNode.TYPE.PROD);
                prodNode.setData(prodCode);
                acctNode.setFather(prodNode);

                CpsAcctProdMast prod = cpsAcctProdMastQueryDAO.find(CpsAcctProdMast.class, prodCode);
                if (prod != null) {
                    String lineId = prod.getProdLineId();
                    if (StringUtils.isNotEmpty(lineId)) {

                        // 创建产品线节点
                        AcctTreeNode lineNode = new AcctTreeNode(AcctTreeNode.TYPE.LINE);
                        lineNode.setData(lineId);
                        prodNode.setFather(lineNode);
                    }
                }
            }
        }
        return acctNode;
    }

}
